import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/project_same_time_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/file_management/three_same_time/three_same_time_details/three_same_time_details_controller.dart';

/// 三同时详情
class ThreeSameTimeDetailsPage extends StatelessWidget {
  ThreeSameTimeDetailsPage({super.key});

  final controller = Get.put(ThreeSameTimeDetailsController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '项目详情',
            ),
            body: Container(
              alignment: Alignment.topCenter,
              color: const Color(0xffF5F5F6),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Container(
                      height: 38.w,
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.only(left: 16.w),
                      child: Text('基本信息', style: TextStyle(fontSize: 12.sp, color: const Color(0xFF7C7D93))),
                    ),
                    Container(
                      height: 2.w,
                      color: const Color(0xFF587DF5),
                    ),
                    Container(
                      color: Colors.white,
                      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 15.w),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(controller.entity.projectName,
                              style: TextStyle(
                                fontSize: 16.sp,
                                fontWeight: FontWeight.w500,
                                color: const Color(0xFF262F38),
                              )),
                          AppGaps.vGap10,
                          _getTextView(title: '项目类型：：', value: controller.entity.projectTypeName),
                          AppGaps.vGap10,
                          _getTextView(title: '总体项目开始时间：', value: controller.entity.start),
                          AppGaps.vGap10,
                          _getTextView(title: '总体项目结束时间：', value: controller.entity.end),
                          AppGaps.vGap10,
                          _getTextView(title: '当前阶段：', value: controller.entity.nowStatus),
                        ],
                      ),
                    ),
                    ListView.builder(
                        itemCount: controller.dataList.length,
                        shrinkWrap: true,
                        physics: const BouncingScrollPhysics(),
                        itemBuilder: (context, index) {
                          return _getTrainView(controller.dataList[index]);
                        }),
                  ],
                ),
              ),
            ),
          );
        });
  }

  ///获取节点信息
  Widget _getTrainView(ProjectSameTimeEntity value) {
    return Column(
      children: [
        Container(
          height: 38.w,
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 16.w),
          child: Text(value.statusName!, style: TextStyle(fontSize: 12.sp, color: const Color(0xFF7C7D93))),
        ),
        Container(
          height: 2.w,
          color: const Color(0xFF587DF5),
        ),
        Container(
          color: Colors.white,
          padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 15.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _getTextView(title: '所属阶段：', value: value.projectNowStatusName!),
              AppGaps.vGap10,
              _getTextView(title: '开始时间：', value: value.projectNowBeginDate!),
              AppGaps.vGap10,
              _getTextView(title: '结束时间：', value: value.projectNowEndDate!),
              AppGaps.vGap10,
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    width: 130.w,
                    child: Text(
                      '资料台账:',
                      style: TextStyle(
                        fontSize: 13.sp,
                        height: 1.2,
                        fontWeight: FontWeight.w400,
                        color: const Color(0xffA5A6B5),
                      ),
                    ),
                  ),
                  Expanded(
                    child: ListView.separated(
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        itemBuilder: (context, index) {
                          return _getAttachmentView(value.fileList![index]);
                        },
                        separatorBuilder: (context, index) => AppGaps.vGap12,
                        itemCount: value.fileList!.length),
                  )
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  /// 获取附件列显示
  Widget _getAttachmentView(String url) {
    //本地文件名
    final String currUrlName = url.toString().substring(25);
    return Text(
      currUrlName,
      style: TextStyle(
        fontSize: 13.sp,
        color: const Color(0xFF5C6DFF),
      ),
    ).clickOnce(() {
      //打开文件
      FileUtils.openFile(Interface.fileUrl + url);
    });
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value, Color? valueColor}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 130.w,
          child: Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA5A6B5),
            ),
          ),
        ),
        Expanded(
            child: Text(
          value,
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: valueColor ?? const Color(0xff495061),
          ),
        ))
      ],
    );
  }
}
